home *** CD-ROM | disk | FTP | other *** search
/ Amiga Game-Power / Amiga Game-Power.iso / pd mix ii / access / backup / dir.c < prev    next >
C/C++ Source or Header  |  1994-05-20  |  4KB  |  176 lines

  1.  
  2. #include "dir.h"
  3. #include "backup.h"
  4.  
  5.  
  6. extern char *strsave ();
  7.  
  8.  
  9. struct dir_st *
  10. getdir ( path )
  11. char *path;
  12. {
  13.     struct DPTR *dptr;
  14.     int dir;
  15.     char *name;
  16.     struct DateStamp date;
  17.     struct dir_st **pp;
  18.     struct dir_st *p;
  19.     struct dir_st *next;
  20.     struct dir_st *head;
  21.  
  22.     dptr = dopen ( path , &dir );
  23.     if ( dptr == NULL ) {
  24.         printf ( "Failed to open directory '%s'\n" , path );
  25.         return ( NULL );
  26.     }
  27.     if ( ! dir ) {
  28.         printf ( "'%s' is a file, not a directory\n" , path );
  29.         return ( NULL );
  30.     }
  31.     head = (struct dir_st *) malloc ( sizeof ( struct dir_st ) );
  32.     if ( head == NULL ) {
  33.         puts ( "Out of memory!" );
  34.         return ( NULL );
  35.     }
  36.     head->filename = strsave ( path );
  37.     head->dir = dir;
  38.     head->date = dptr->fib->fib_Date;
  39.     head->next = NULL;
  40.     pp = &head->next;
  41.     while ( dnext ( dptr , &name , &dir , &date ) ) {
  42.  
  43.         /* ignore .dobackup files - dont want to accidently override */
  44.         /* any existing file */
  45.  
  46.         if ( strcmp ( name , DO_BACKUP ) == 0 )
  47.             continue;
  48.  
  49.         p = (struct dir_st *) malloc ( sizeof ( struct dir_st ) );
  50.         if ( p == NULL
  51.         ||  ( p->filename = strsave ( name ) ) == NULL ) {
  52.             for ( p = head; p != NULL; p = next ) {
  53.                 next = p->next;
  54.                 free ( p->filename );
  55.                 free ( p );
  56.             }
  57.             puts ( "Out of memory!" );
  58.             return ( NULL );
  59.         }
  60.         p->dir = dir;
  61.         p->date = date;
  62.         p->next = NULL;
  63.         *pp = p;
  64.         pp = &p->next;
  65.     }
  66.     dclose ( dptr );
  67.     return ( head );
  68. }
  69.  
  70.  
  71. void
  72. freedir ( dir )
  73. struct dir_st *dir;
  74. {
  75.     struct dir_st *p , *next;
  76.  
  77.     for ( p = dir; p != NULL; p = next ) {
  78.         next = p->next;
  79.         free ( p->filename );
  80.         free ( p );
  81.     }
  82. }
  83.  
  84.  
  85. /*
  86.  * Disk directory routines
  87.  *
  88.  * dptr = dopen(name, stat)
  89.  *    struct DPTR *dptr;
  90.  *    char *name;
  91.  *    int *stat;
  92.  *
  93.  * dnext(dptr, name, stat, date)
  94.  *    struct DPTR *dptr;
  95.  *    char **name;
  96.  *    int  *stat;
  97.  *    struct DateStamp *date;
  98.  *
  99.  * dclose(dptr)                  -may be called with NULL without harm
  100.  *
  101.  * dopen() returns a struct DPTR, or NULL if the given file does not
  102.  * exist.  stat will be set to 1 if the file is a directory.  If the
  103.  * name is "", then the current directory is openned.
  104.  *
  105.  * dnext() returns 1 until there are no more entries.  The **name and
  106.  * *stat are set.  *stat = 1 if the file is a directory.
  107.  *
  108.  * dclose() closes a directory channel.
  109.  *
  110.  */
  111.  
  112. struct DPTR *
  113. dopen(name, stat)
  114. char *name;
  115. int *stat;
  116. {
  117.     struct DPTR *dp;
  118.     int namelen, endslash = 0;
  119.  
  120.     namelen = strlen(name);
  121.     if (namelen && name[namelen - 1] == '/') { 
  122.         name[namelen - 1] = '\0';           
  123.         endslash = 1;
  124.     }
  125.     *stat = 0;
  126.     dp = (struct DPTR *)malloc(sizeof(struct DPTR));
  127.     dp->lock = Lock (name, (LONG)ACCESS_READ);
  128.     if (endslash)
  129.         name[namelen - 1] = '/';
  130.     if (dp->lock == '\0') {
  131.         free (dp);
  132.         return (NULL);
  133.     }
  134.     dp->fib = (struct FileInfoBlock *)
  135.          AllocMem((LONG)sizeof(struct FileInfoBlock), (LONG)MEMF_PUBLIC);
  136.     if (!Examine (dp->lock, dp->fib)) {
  137.         perror (name);
  138.         dclose (dp);
  139.         return (NULL);
  140.     }
  141.     if (dp->fib->fib_DirEntryType >= 0)
  142.         *stat = 1;
  143.     return (dp);
  144. }
  145.  
  146. dnext(dp, pname, stat, date)
  147. struct DPTR *dp;
  148. char **pname;
  149. int *stat;
  150. struct DateStamp *date;
  151. {
  152.     if (dp == NULL)
  153.         return (0);
  154.     if (ExNext (dp->lock, dp->fib)) {
  155.         *stat = (dp->fib->fib_DirEntryType < 0) ? 0 : 1;
  156.         *date = dp->fib->fib_Date;
  157.         *pname = dp->fib->fib_FileName;
  158.         return (1);
  159.     }
  160.     return (0);
  161. }
  162.  
  163.  
  164. dclose(dp)
  165. struct DPTR *dp;
  166. {
  167.     if (dp == NULL)
  168.         return (1);
  169.     if (dp->fib)
  170.         FreeMem (dp->fib, (LONG)sizeof(*dp->fib));
  171.     if (dp->lock)
  172.         UnLock (dp->lock);
  173.     free (dp);
  174.     return (1);
  175. }
  176.